#include "sgl_init.h"   


/**
 * 字库字模 14号
 * 流水灯带传感器长度总动时间全亮保持格式时分秒关于我们联系ABRGSs0123456789.:%
 * 
 * 字库字模 16号
 * 流水灯带控制器ABRGSs0123456789.:%
 * 
 */


sgl_obj_t *button = NULL;
sgl_obj_t *ver_label = NULL;
sgl_obj_t *title_label = NULL;
static uint16_t dis_anim_over = FALSE;

extern void lcd_fill_diff_color(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t *array);
extern void usart1_send_string(char *str);
extern void lcd_back_light_set(u8 val);

static void sgl_anim_power_on(struct sgl_anim *anim, int32_t value);

void panel_flush_area(int16_t x, int16_t y, int16_t w, int16_t h,sgl_color_t *color)
{
    lcd_fill_diff_color(x, y, x + w - 1, y + h - 1, (uint16_t*)color);
}
void uart_put_string(const char *str) 
{
	usart1_send_string((char *)str);
}




void user_sgl_init(void)
{
	#define PANEL_WIDTH 160
	#define PANEL_HEIGHT 80

    static sgl_color_t panel_buffer[PANEL_WIDTH * 10];
    sgl_device_fb_t fb_dev = {
        .xres = PANEL_WIDTH,
        .yres = PANEL_HEIGHT,
        .xres_virtual = PANEL_WIDTH,
        .yres_virtual = PANEL_HEIGHT,
        .flush_area = panel_flush_area,
        .framebuffer = panel_buffer,
        .framebuffer_size = SGL_ARRAY_SIZE(panel_buffer),
    };

    sgl_device_fb_register(&fb_dev);
    sgl_device_log_register(uart_put_string);
	sgl_init();
	/* 设置主页面背景颜色 */
	sgl_obj_set_style(sgl_screen_act(),SGL_STYLE_COLOR,SGL_COLOR(SGL_BLACK));
	
	/* 创建开机标题label */
    title_label = sgl_label_create(NULL);
    sgl_obj_set_pos(title_label, 0, 5);
    sgl_obj_set_size(title_label, 160, 0);
    sgl_obj_set_style(title_label, SGL_STYLE_RADIUS, 20);
	sgl_obj_set_style(title_label, SGL_STYLE_COLOR, SGL_COLOR(sgl_rgb(255, 255, 255)));
	sgl_obj_set_style(title_label,SGL_STYLE_ALPHA,0);
    sgl_obj_set_style(title_label, SGL_STYLE_BG_COLOR, SGL_COLOR(sgl_rgb(255, 255, 255)));
	sgl_obj_set_style(title_label,SGL_STYLE_FONT,SGL_FONT(my_font16));
    sgl_obj_set_style(title_label, SGL_STYLE_TEXT ,SGL_TEXT("流水灯带控制器"));
	sgl_obj_set_style(title_label, SGL_STYLE_TEXT_Y_OFFSET, 4);
	sgl_obj_set_style(title_label,SGL_STYLE_TEXT_COLOR,SGL_COLOR(SGL_BLACK)); 

    /* 创建开机动画-控制标题label大小 */	
  	sgl_anim_t *anim = sgl_anim_create();
	sgl_anim_set_obj(anim,title_label);
	sgl_anim_set_act_delay(anim,0);
	sgl_anim_set_act_duration(anim,2000);
	sgl_anim_set_repeat_cnt(anim,1);
	sgl_anim_set_start_value(anim,0);
	sgl_anim_set_end_value(anim,255);	
	sgl_anim_set_path(anim,sgl_anim_power_on);
	sgl_anim_set_path_algo(anim,SGL_ANIM_PATH_LINEAR);
	sgl_anim_start(anim);

	/* 创建版本label,先不显示 */
    ver_label = sgl_label_create(NULL);
    sgl_obj_set_pos(ver_label, 110, 60);
    sgl_obj_set_size(ver_label, 50, 20);
    sgl_obj_set_style(ver_label, SGL_STYLE_FONT, SGL_FONT(my_font10));
    sgl_obj_set_style(ver_label, SGL_STYLE_TEXT, SGL_TEXT(FIRMWARE_STRING));
	sgl_obj_set_style(ver_label, SGL_STYLE_TEXT_COLOR, SGL_COLOR(SGL_BLACK));

	sgl_task_handle();
	delay_ms(10);
	/* 先显示后开背光，防止闪屏问题 */
	lcd_back_light_set(100);
    while(1)
	{
		sgl_task_handle();
		delay_ms(30);
		if(dis_anim_over == TRUE)
		{
			delay_ms(1000);
			/* 删除已经显示对象 */
			sgl_obj_delete(title_label);
			sgl_obj_delete(ver_label);
			sgl_task_handle();sgl_task_handle();
			dis_anim_over = FALSE;
			break;
		}
	}
}

static void sgl_anim_power_on(struct sgl_anim *anim, int32_t value)
{
	/* 动态设置按键label的尺寸 */
	sgl_obj_set_size(title_label, 160, value * 50 / 255);
	/* label尺寸修改后需要重新设置圆角半径 */
	sgl_obj_set_style(title_label, SGL_STYLE_RADIUS, 15);
    /* 动态设置按键label的透明度 */
	sgl_obj_set_style(title_label,SGL_STYLE_ALPHA,value);

    /* 动画结束显示版本号label */
    if(value == 255)
    {
		sgl_obj_set_style(ver_label, SGL_STYLE_TEXT_COLOR, SGL_COLOR(SGL_WHITE));
		dis_anim_over = TRUE;
	}
}

